package com.xkstream.client.presenter.kt

import com.xkstream.client.api.LiveApi
import com.xkstream.client.api.LiveService
import com.xkstream.client.api.VersionApi
import com.xkstream.client.api.VersionService
import com.xkstream.client.api.kt.MessageApi
import com.xkstream.client.api.kt.MessageService
import com.xkstream.client.base.BaseData
import com.xkstream.client.base.BasePresenterImpl
import com.xkstream.client.entity.FirVersion
import com.xkstream.client.entity.Game
import com.xkstream.client.entity.Version
import com.xkstream.client.entity.kt.MessagesData
import com.xkstream.client.rx.RxHelper
import com.xkstream.client.rx.RxObserver

/**
 * Created by xiaoyi on 2017-11-3.
 */
class AnchorStatePresenter() : BasePresenterImpl(), AnchorStateContract.Presenter {
    var view: AnchorStateContract.View? = null
    var messageService: MessageService? = null
    var liveService: LiveService? = null
    var versionService :VersionService?=null

    constructor(view: AnchorStateContract.View) : this() {
        this.view = view
        this.view?.setPresenter(this)
        this.messageService = MessageApi.getInstance().service
        this.liveService = LiveApi.getInstance().service
        this.versionService = VersionApi.getInstance().service
    }

    override fun getUnreadMessage(params: Map<String, String>) {
        //view?.showLoading()
        messageService?.getMessageList(params)
                ?.compose(RxHelper.toUI())
                ?.subscribe(object : RxObserver<BaseData<MessagesData>>(this) {
                    override fun onFail(message: String?) {
                        // view?.hideLoading()
                        view?.showError(message)
                    }

                    override fun onSuccess(t: BaseData<MessagesData>) {
                        //view?.hideLoading()
                        view?.showUnReadMessage(t)
                    }
                })
    }

    override fun anchorOn(params: Map<String, String>) {
        view?.showLoading()
        liveService?.anchorOnOff(params)
                ?.compose(RxHelper.toUI())
                ?.subscribe(object : RxObserver<BaseData<Any?>>(this) {
                    override fun onFail(message: String) {
                        view?.hideLoading()
                        view?.showError(message)
                    }

                    override fun onSuccess(t: BaseData<Any?>) {
                        view?.hideLoading()
                        view?.showAnchorOnResult(t)
                    }
                })
    }

    override fun checkVersion() {
        view?.showLoading()
        versionService?.getLatestVersionFromFir(VersionService.FIR_APITOKEN)
                ?.compose(RxHelper.toUI())
                ?.subscribe(object :RxObserver<FirVersion>(this){
                    override fun onFail(message: String?) {
                        view?.hideLoading()
                        view?.showError(message)
                    }

                    override fun onSuccess(t: FirVersion?) {
                        view?.hideLoading()
                        view?.showFirVersion(t)
                    }
                })
    }
}